#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include "beepled.h"
#include "udpnet.h"
#include "projsysnet.h"
#include<stdio.h>
#include <sys/socket.h>
#include <sys/types.h>          /* See NOTES */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define DEST_IP "192.168.1.136"
#define DEST_PORT 15000
#define HOST_IP "192.168.1.137"
#define HOST_PORT 11000

extern int sendsocketfd;
extern int recvsocketfd;
extern struct sockaddr_in hostaddr;
extern struct sockaddr_in dest_addr;
extern int udpsendstr(const char* data);
extern void udprecv_routine(void);



int udpsendstr(const char* data)
{
    pthread_detach(pthread_self());
    char buf[1024] = {0};
    printf("senddata: %s\n",data);
    strcpy(buf,data);

    int ret = sendto(sendsocketfd, buf, strlen(buf),0,
                (struct sockaddr*)&dest_addr,sizeof(dest_addr));
    if(ret == -1)
    {
        perror("sendto error");
        return 1;
    }

    printf("sendto ret:%d\n",ret);
    sleep(5);
    return 0;
}



void udprecv_routine(void)
{
    printf("recvtid\n");
    //2 接收数据
    while (1)
    {

       char buf[1024] = {0};
       struct sockaddr_in addr;
       int addrlen = sizeof(addr); //一定要赋值

       int ret = recvfrom(recvsocketfd, buf, sizeof(buf),0,(struct sockaddr*)&addr,&addrlen);
       if(ret == -1)
       {
            perror("recvfrom error");
            break;
       }

       if(!strcmp(buf,"turnonaircondition"))
       {
            lv_switch_set_state(sw, true);
       }
       if(!strcmp(buf,"turnoffaircondition"))
       {
            lv_switch_set_state(sw, false);
       }

       printf("IP:[%s] Port:[%d]recvfrom:%s ret:%d\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port), buf,ret);

       //转发数据
    }
}